import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layout/index.vue'),
    redirect: '/dashboard',
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '工作台',
          icon: 'House'
        }
      },
      {
        path: '/material',
        name: 'Material',
        component: () => import('@/views/material/index.vue'),
        meta: {
          title: '素材库',
          icon: 'Picture'
        }
      },
      {
        path: '/upload',
        name: 'Upload',
        component: () => import('@/views/upload/index.vue'),
        meta: {
          title: '上传素材',
          icon: 'Upload'
        }
      },
      {
        path: '/copywriting',
        name: 'Copywriting',
        component: () => import('@/views/copywriting/index.vue'),
        meta: {
          title: 'AI文案',
          icon: 'Edit'
        }
      },
      {
        path: '/generate',
        name: 'Generate',
        component: () => import('@/views/generate/index.vue'),
        meta: {
          title: '图片生成',
          icon: 'MagicStick'
        }
      },
      {
        path: '/export',
        name: 'Export',
        component: () => import('@/views/export/index.vue'),
        meta: {
          title: '图文导出',
          icon: 'Download'
        }
      },
      {
        path: '/tags',
        name: 'Tags',
        component: () => import('@/views/tags/index.vue'),
        meta: {
          title: '标签管理',
          icon: 'CollectionTag'
        }
      }
    ]
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('@/layout/admin.vue'),
    redirect: '/admin/dashboard',
    meta: {
      requiresAuth: true,
      requiresAdmin: true
    },
    children: [
      {
        path: '/admin/dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/dashboard/index.vue'),
        meta: {
          title: '管理后台',
          icon: 'DataBoard'
        }
      },
      {
        path: '/admin/users',
        name: 'AdminUsers',
        component: () => import('@/views/admin/users/index.vue'),
        meta: {
          title: '用户管理',
          icon: 'User'
        }
      },
      {
        path: '/admin/materials',
        name: 'AdminMaterials',
        component: () => import('@/views/admin/materials/index.vue'),
        meta: {
          title: '素材管理',
          icon: 'FolderOpened'
        }
      },
      {
        path: '/admin/ai-config',
        name: 'AdminAiConfig',
        component: () => import('@/views/admin/ai-config/index.vue'),
        meta: {
          title: 'AI配置',
          icon: 'Setting'
        }
      },
      {
        path: '/admin/statistics',
        name: 'AdminStatistics',
        component: () => import('@/views/admin/statistics/index.vue'),
        meta: {
          title: '统计分析',
          icon: 'TrendCharts'
        }
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面不存在'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router